﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Windows;

namespace XMM.Objects
{
    public class Genre : Object
    {
        /// <summary>
        /// Database-Record ID
        /// </summary>
        public int GenreID { get; set; }

        /// <summary>
        /// Genre-Name.
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Description (Summary).
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// Initialize a new Genre-Object with blank values.
        /// </summary>
        public Genre()
        {
            blankValues();
        }

        /// <summary>
        /// Initialize a new Genre-Object and load info from database.
        /// </summary>
        /// <param name="iGenreID">(int) Target-Genre Database-ID.</param>
        public Genre(int iGenreID)
        {
            blankValues();

            Load(iGenreID);
        }

        /// <summary>
        /// Blank this object's values.
        /// </summary>
        private void blankValues()
        {
            GenreID = 0;
            Name = String.Empty;
            Description = String.Empty;
        }

        public override string ToString()
        {
            return Name;
        }


        /// <summary>
        /// Load selected Genre from databse.
        /// </summary>
        /// <param name="iGenreID">(int) Target-Genre-ID.</param>
        /// <returns>[true] if Genre was loaded successfully.</returns>
        public bool Load(int iGenreID)
        {
            try
            {
                XMMDBDataContext dbConnection = new XMMDBDataContext();

                var vGenres = from genre in dbConnection.t_genres where genre.ID == iGenreID select genre;

                if (vGenres != null)
                {
                    foreach (t_genres vGenre in vGenres)
                    {
                        GenreID = vGenre.ID;
                        Name = vGenre.Name;
                        if (vGenre.Description != null)
                            Description = vGenre.Description;
                    }

                    return true;
                }
            }
            catch (SqlException ex)
            {
                MessageBox.Show("Fehler beim Laden des Genres: " + ex.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            return false;
        }

        /// <summary>
        /// Get a List of all available Genres.
        /// </summary>
        /// <returns>[List(Objects.Genre)] All available Genres.</returns>
        public static List<Objects.Genre> getAllGenres()
        {
            List<Objects.Genre> lGenres = new List<Genre>();

            try
            {
                XMMDBDataContext dbConnection = new XMMDBDataContext();

                var vGenreIDs = from genre in dbConnection.t_genres select genre.ID;

                if (vGenreIDs != null)
                {
                    foreach (int iGenreID in vGenreIDs)
                    {
                        Objects.Genre oGenre = new Genre(iGenreID);
                        if (oGenre != null && oGenre.GenreID > 0)
                            lGenres.Add(oGenre);
                    }
                }
            }
            catch (SqlException ex)
            {
                MessageBox.Show("Fehler beim Abrufen der verfügbaren Genres: " + ex.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            return lGenres;
        }

        /// <summary>
        /// Save new Genre to database.
        /// </summary>
        /// <returns>[true] if Genre was saved successfully, otherwise [false].</returns>
        public bool SaveNew()
        {
            if (Name != String.Empty)
            {
                try
                {
                    XMMDBDataContext dbConnection = new XMMDBDataContext();

                    t_genres oNewGenre = new t_genres();
                    oNewGenre.Name = Name;
                    oNewGenre.Description = Description;

                    dbConnection.t_genres.InsertOnSubmit(oNewGenre);
                    dbConnection.SubmitChanges();

                    return true;
                }
                catch (SqlException ex)
                {
                    MessageBox.Show("Fehler beim Speichern des Genres: " + ex.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }

            return false;
        }

        /// <summary>
        /// Save changes to existing genre.
        /// </summary>
        /// <returns></returns>
        public bool SaveChanges()
        {
            if (GenreID != 0 && Name != String.Empty)
            {
                try
                {
                    XMMDBDataContext dbConnection = new XMMDBDataContext();

                    var vGenre = (from genre in dbConnection.t_genres where genre.ID == GenreID select genre).Single();

                    vGenre.Name = Name;
                    vGenre.Description = Description;

                    dbConnection.SubmitChanges();

                    return true;
                }
                catch (SqlException ex)
                {
                    MessageBox.Show("Fehler beim Speichern der Änderungen des Genres \"" + Name + "\": " + ex.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }

            return false;
        }

    }
}
